package com.itcast.clientx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

public class TableData {

    /**
     *
     * @param args
     */
    public static void main(String[] args) throws Exception{

        /**
         * 依次定位到 表格[#vital_river_table] 表体[#vital_river_Tmpl] 表格的行[.item_info]
         * 此处获取到的是表体的所有行
         */
        String html = getHTML("http://xxfb.mwr.cn/portal/vitalRiverInfo");
        JSONObject json =JSON.parseObject(html);
        JSONArray array = json.getJSONArray("result");
        Document document = Jsoup.parse(new URL("http://xxfb.mwr.cn/"), 5000);
        /*----------------------------------------------------------------------------------------------*/
        /**
         * 依次定位到 表格[#vital_river_table] 表头[thead] 表格的行[.item] 行里的列[td]
         * 此处获取到的是表头的所有列
         */
        Elements thead_tds = document.select("#vital_river_table thead .item td");

        for (Element thead_td : thead_tds){

            String text = thead_td.text().trim();

            System.out.print(text+"\t");

        }
        /**
         * 输出完表头 换行
         */
        System.out.println();
        /*---------------------------------------------------------------------------------------------------*/
        for (int i = 0; i < array.size(); i++) {
            JSONObject tr = (JSONObject) array.get(i);
            String stnm = tr.get("stnm").toString().trim();
            System.out.print(stnm+"\t");
            String webStlc = tr.get("webStlc").toString().trim();
            System.out.print(webStlc+"\t");
            String rvnm = tr.get("rvnm").toString().trim();
            System.out.print(rvnm+"\t");
            String zl = tr.get("zl").toString().trim();
            System.out.print(zl+"\t");
            String ql = tr.get("ql").toString().trim();
            System.out.print(ql+"\t");
            String tm = tr.get("tm").toString().trim();
            System.out.print(tm+"\t");
            String wrz = tr.get("wrz").toString().trim();
            System.out.println();
        }
        /*for (Element tbody_tr : tbody_trs){
            *//**
             * 当前行的所有列
             *//*
            Elements tds = tbody_tr.select("td");
            for (Element td : tds) {
                System.out.print(td.text()+"\t");
            }
            System.out.println();
        }*/
        System.out.println(json);
    }
    /**
     * 获取HTML数据
     * @param url
     * @return
     */
    public static String getHTML(String url) {
        CloseableHttpClient client = HttpClients.createDefault();


        CloseableHttpResponse response = null;

        try {


            HttpGet request = new HttpGet(url);

            response = client.execute(request);

            int status = response.getStatusLine().getStatusCode();
            System.out.println("status==  "+status);
            if(status == 200){
                HttpEntity entity = response.getEntity();
                if(entity == null){
                    return null;
                }

                String string = EntityUtils.toString(entity, "utf8");
                System.out.println(string);
                return string;
            }
            if(status == 404){
                return "404";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }
}
